package com.czwj.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.OperateResult;
import com.czwj.entities.ProjectItemChild;
import com.czwj.entities.SafetyInspection;
import com.czwj.entities.UserInfo;
import com.czwj.service.SafetyInspectionService;
import com.czwj.util.GsonUtil;

@Controller
public class SafetyInspectionAction {
	@Resource
	SafetyInspectionService safetyInspectionService;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@RequestMapping(value = "/existinspection", method = RequestMethod.GET)
	@ResponseBody
	public List<String> existinspection(@RequestParam(value = "projectitemid") int projectitemid,
			@RequestParam(value = "yearmonth") String yearmonth) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemid", projectitemid);
		map.put("yearmonth", yearmonth);
		List<String> list = safetyInspectionService.existinspection(map);
//		for (String string : list) {
//			System.err.println(string);
//		}
		
		return list;
	}
	
	@RequestMapping(value = "/safetyinspection/findoneday", method = RequestMethod.GET)
	@ResponseBody
	public List<SafetyInspection> findoneday(
			@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "inspectiondate", required = false) Date inspectiondate){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemid", projectitemid);
		map.put("inspectiondate", sdf.format(inspectiondate));
		List<SafetyInspection> list = safetyInspectionService.findonday(map);
		return list;
	}
	
	@RequestMapping(value = "/selectTosafeInfo", method = RequestMethod.GET)
	@ResponseBody
	public List<SafetyInspection> selectTosafeInfo(@RequestParam(value = "inspectiondate", required = false) String inspectiondate){
		Map<String, Object> map = new HashMap<String, Object>();
		Date date = new Date(Long.valueOf(inspectiondate));
		map.put("inspectiondate", sdf.format(date));
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("companyid",userInfo.getCompanyid());
		List<SafetyInspection> list = safetyInspectionService.selectTosafeInfo(map);
		return list;
	}
	
	@SystemControllerLog(description = "修改安全巡检") 
	@RequestMapping(value = "/safetyinspectionSave.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult safetyinspectionSave(@RequestParam(value = "arrstr", required = true) String arrstr,
			@RequestParam(value = "inspectiondate", required = true) String inspectiondate,
			@RequestParam(value = "images", required = false) MultipartFile[] images,
			@RequestParam(value = "status", required = true) Integer status,
			HttpServletRequest request) throws Exception{
		OperateResult result = new OperateResult();
		List<SafetyInspection> list = GsonUtil.jsonStrToObjects(arrstr, SafetyInspection.class);
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = simpleDateFormat.parse(inspectiondate);
		int count = safetyInspectionService.insert(list,images,date1,userInfo.getId(),request.getRemoteAddr(),status);
		result.setSuccess(count>0);
		return result;
	}
	
	
	@SystemControllerLog(description = "处理安全巡检") 
	@RequestMapping(value = "/safetyinspectionUpdate.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult safetyinspectionUpdate(@RequestBody SafetyInspection safe) throws Exception{
		OperateResult result = new OperateResult();
		Date date = safe.getInspectiondate();
		int count = safetyInspectionService.updateStatus(date);
		result.setSuccess(count>0);
		return result;
	}
}
